package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author asus
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2024-09-19 11:48:57
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    AttrValueMapper attrValueMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    PaymentTypeMapper paymentTypeMapper;
    @Autowired
    LeaseTermMapper leaseTermMapper;



    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;



    @Override
    public IPage<RoomItemVo> pageItemInfo(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItemInfo(roomItemVoPage, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        //查询房间的基本信息
        RoomInfo roomInfo = this.getById(id);
         BeanUtils.copyProperties(roomInfo, roomDetailVo);
        //根据公寓的id查询公寓的基本信息 ->查询的是房间所属的公寓
        Long apartmentId = roomInfo.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);


        //根据房间的id查询图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectListByIdAndType(roomInfo.getId(), ItemType.ROOM);
        //根据房间的id查询属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(roomInfo.getId());
        //根据房间的id查询配套信息
       // List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectListByRoomId(roomInfo.getId());
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(roomInfo.getId());
        //根据房间的id查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(roomInfo.getId());
        //根据房间的id查询支付类型
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(roomInfo.getId());
        //根据房间的id查询租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(roomInfo.getId());


        // 把数据保存到RoomDetailVo
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);



        return roomDetailVo;
    }

    @Override
    public void saveOrUpdateInfo(RoomSubmitVo roomSubmitVo) {
        //房间的id
        Long id = roomSubmitVo.getId();

        //添加和修改房间的基本信息-->如果是添加 自动返回最新的id
        this.saveOrUpdate(roomSubmitVo);

        if(id!=null){
            //修改-->删除和房间建立关系的关系表数据
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.ROOM));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        }

        //添加新的关系
        List<GraphVo> graphVoList=roomSubmitVo.getGraphVoList();
         if(!CollectionUtils.isEmpty(graphVoList)){
              List<GraphInfo> graphInfos=new ArrayList<>();
             for (GraphVo graphVo : graphVoList) {
                 GraphInfo graphInfo=new GraphInfo();
                 graphInfo.setName(graphVo.getName());
                 graphInfo.setUrl(graphVo.getUrl());
                 graphInfo.setItemId(roomSubmitVo.getId());
                 graphInfo.setItemType(ItemType.ROOM);
                 graphInfos.add(graphInfo);
             }
             graphInfoService.saveBatch(graphInfos);
         }


        List<Long> attrValueIds=roomSubmitVo.getAttrValueIds();

         if(!CollectionUtils.isEmpty(attrValueIds)){
             List<RoomAttrValue> roomAttrValues=new ArrayList<>();
             for (Long attrValueId : attrValueIds) {
                 RoomAttrValue roomAttrValue=RoomAttrValue.builder().build();
                 roomAttrValue.setRoomId(roomSubmitVo.getId());
                 roomAttrValue.setAttrValueId(attrValueId);
                 roomAttrValues.add(roomAttrValue);
             }
             roomAttrValueService.saveBatch(roomAttrValues);
         }


        List<Long> facilityInfoIds=roomSubmitVo.getFacilityInfoIds();


        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> roomFacilities=new ArrayList<>();
            for (Long facilitieId : facilityInfoIds) {
                RoomFacility roomFacility=RoomFacility.builder().build();
                roomFacility.setRoomId(roomSubmitVo.getId());
                roomFacility.setFacilityId(facilitieId);
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }




        //4.保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        //5.保存新的paymentTypeList
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        //6.保存新的leaseTermList
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }



    }

    @Override
    public void removeRoomById(Long id) {
        //1.删除RoomInfo
        super.removeById(id);

        //2.删除graphInfoList
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //3.删除attrValueList
        LambdaQueryWrapper<RoomAttrValue> attrQueryWrapper = new LambdaQueryWrapper<>();
        attrQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(attrQueryWrapper);

        //4.删除facilityInfoList
        LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(facilityQueryWrapper);

        //5.删除labelInfoList
        LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(labelQueryWrapper);

        //6.删除paymentTypeList
        LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
        paymentQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(paymentQueryWrapper);

        //7.删除leaseTermList
        LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
        termQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(termQueryWrapper);
    }

}





